//
//  CSCreatingVC.m
//  CreateWithSuper
//
//  Created by chenjunbo on 2024/6/19.
//  Copyright © 2024 CS. All rights reserved.
//

@import Lottie;
#import "CSCreatingVC.h"
#import "CSResultController.h"
#import "CSNetworkHelper.h"
#import "ADModule.h"

@interface CSCreatingVC ()

@property (weak, nonatomic) IBOutlet LOTAnimationView *animationView;
@property (weak, nonatomic) IBOutlet UILabel *progressLabel;
@property (nonatomic, strong) NSTimer *requestTimer;

@end

@implementation CSCreatingVC

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    if (self.requestTimer){
        [self.requestTimer invalidate];
        self.requestTimer = nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    id traget = self.navigationController.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer * pan = [[UIPanGestureRecognizer alloc]initWithTarget:traget action:nil];
    [self.view addGestureRecognizer:pan];
    
    [self.animationView playFromProgress:0 toProgress:0.5 withCompletion:^(BOOL animationFinished) {
            
    }];
    self.animationView.loopAnimation = YES;

    if (self.isLocal){
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
        CSResultController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSResultController"];
        vc.isLimited = self.isLimited;
        vc.createStr = self.createStr;
        vc.isLocal = self.isLocal;
        vc.resultImgStr = [NSString stringWithFormat:@"http://qrscanonetool.com/ai/image/%@",self.createStr];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        self.requestTimer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {

            [[CSNetworkHelper standardInstance]requestTaskRequest:self.taskID callback:^(NSDictionary *  _Nonnull result) {
                
                NSLog(@"%@",result);
                
                if (result[@"task"]){
                    
                    NSDictionary *taskDict = result[@"task"];
                    
                    self.progressLabel.text = [NSString stringWithFormat:@"%@%%",taskDict[@"progress_percent"]];
                    
                }
                
                NSArray *imagesArr = result[@"images"];
                
                if (imagesArr.count > 0){
                    
                    [self.animationView stop];
                    NSDictionary *imageDict = result[@"images"][0];
                    [self.requestTimer invalidate];
                    self.requestTimer = nil;
                    
                    self.animationView.loopAnimation = NO;
                    [self.animationView playFromProgress:0.5 toProgress:1 withCompletion:^(BOOL animationFinished) {
                        
                        if (animationFinished){
                            [[ADModule standardInstance]showInterAdWithVC:self dismiss:^{
                                UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Create" bundle:nil];
                                CSResultController *vc = [storyboard instantiateViewControllerWithIdentifier:@"CSResultController"];
                                vc.isLimited = self.isLimited;
                                vc.createStr = self.createStr;
                                vc.resultImgStr = imageDict[@"image_url"];
                                [self.navigationController pushViewController:vc animated:YES];
                            }];

                        }
                        
                    }];
                    
                }
                
            }];
            
    //        [[CSNetworkHelper standardInstance]requestTaskRequest:self.taskID callback:^(NSString * _Nonnull result) {
    //
    //            NSLog(@"%@",result[@"task"]);
    //
    //        }];
            
        }];
        
        [[NSRunLoop currentRunLoop]addTimer:self.requestTimer forMode:NSRunLoopCommonModes];
    }
    
}

@end
